package com.oriental.manage.service.specially.Impl;

import com.oriental.check.commons.util.BeanMapperUtil;
import com.oriental.manage.dao.specially.EnterpriseMchntBaseInfoMapper;
import com.oriental.manage.model.specially.EnterpriseMchntBaseInfo;
import com.oriental.manage.pojo.specially.BatchMessage;
import com.oriental.manage.service.specially.BatchMessageProcess;
import com.oriental.riskfront.api.enums.MessageStatus;
import com.oriental.riskfront.api.model.specially.EnterpriseMchntBaseInfoDto;
import com.oriental.riskfront.api.service.DataDictInterface;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jinxin on 2017/4/26.
 */
@Service
@Slf4j
public class EnterpriseMchntBaseInfoProcessImpl implements BatchMessageProcess<EnterpriseMchntBaseInfo,EnterpriseMchntBaseInfoDto>{

    @Autowired
   EnterpriseMchntBaseInfoMapper enterpriseMchntBaseInfoMapper;

    @Autowired
    private DataDictInterface dataDictInterface;

    @Override
    public EnterpriseMchntBaseInfo queryDetail(BatchMessage batchMessage) {
        EnterpriseMchntBaseInfo enterpriseMchntBaseInfo = new EnterpriseMchntBaseInfo();
        enterpriseMchntBaseInfo.setId(batchMessage.getSubmitMsgId());
        EnterpriseMchntBaseInfo enterpriseMchntBaseInfo1=enterpriseMchntBaseInfoMapper.searchEnterpriseMchntBaseInfo(enterpriseMchntBaseInfo).get(0);
        String openT= enterpriseMchntBaseInfo1.getOpenType();
        Map<String,String> mapNet= (Map<String,String>)dataDictInterface.getData("CLEARING_NETWORK");
        Map<String,String> mapOpen=(Map<String,String>)dataDictInterface.getData("OPEN_SERVICE_TYPE");
        if(StringUtils.isNotBlank(openT)) {
            String [] netTypeArr=openT.split(",");
            StringBuilder netString=new StringBuilder();
            for (String net : netTypeArr
                    ) {
                netString.append(mapOpen.get(net)).append(",");
            }
          enterpriseMchntBaseInfo1.setOpenType(netString.substring(0,netString.length()-1));
        }
       String network= enterpriseMchntBaseInfo1.getNetworkType();
        if(StringUtils.isNotBlank(network)) {
            String [] netTypeArr=network.split(",");
            StringBuilder netString=new StringBuilder();
            for (String net : netTypeArr
                    ) {
                netString.append(mapNet.get(net)).append(",");
            }
          enterpriseMchntBaseInfo1.setNetworkType(netString.substring(0,netString.length()-1));
        }
       String occ= enterpriseMchntBaseInfo1.getOccurarea();
        if(StringUtils.isNotBlank(occ)){
            String [] occTypeArr=occ.split(",");
            StringBuilder occString=new StringBuilder();
            List<Map<String, Object>> dataAddress = dataDictInterface.getDataAddress("ADDRESS_Select");
            Map<String,String> areaMap = new HashMap<>();
            for (Map<String,Object> m:dataAddress
                    ) {
                areaMap.put((String)m.get("areaCode"),(String)m.get("areaName"));
            }
            for ( String o: occTypeArr
                 ) {
                occString.append(areaMap.get(o)).append(",");
            }
            enterpriseMchntBaseInfo1.setOccurarea(occString.substring(0,occString.length()-1));
        }
        return enterpriseMchntBaseInfo1;
    }

    @Override
    public List<EnterpriseMchntBaseInfoDto> query(List<BatchMessage> list) {
        List<String> ids = new ArrayList<>();
        for (BatchMessage b: list
                ) {
            ids.add(b.getSubmitMsgId());
        }
        List<EnterpriseMchntBaseInfo> speciallyMchntBaseInfoDeleteList= enterpriseMchntBaseInfoMapper.searchEnterpriseMchntBaseInfoFroIds(ids);
        List<EnterpriseMchntBaseInfoDto> speciallyMchntBaseInfoDeleteDtoList= BeanMapperUtil.mapList(speciallyMchntBaseInfoDeleteList,EnterpriseMchntBaseInfoDto.class);
        return speciallyMchntBaseInfoDeleteDtoList;
    }


    @Override
    public void auditFail(List<BatchMessage> batchMessage) {
        for (BatchMessage b:batchMessage
             ) {
            EnterpriseMchntBaseInfo enterpriseMchntBaseInfo = new EnterpriseMchntBaseInfo();
            enterpriseMchntBaseInfo.setUploadStatus(MessageStatus.REJECT.getCode());
            enterpriseMchntBaseInfo.setAuditFailNote(b.getContent());
            enterpriseMchntBaseInfo.setId(b.getSubmitMsgId());
            enterpriseMchntBaseInfoMapper.updateEnterpriseMchntBaseInfo(enterpriseMchntBaseInfo);
        }
    }

    @Override
    public void callback(String status, List<BatchMessage> batchMessage) {
        for (BatchMessage b:batchMessage
                ) {
            EnterpriseMchntBaseInfo enterpriseMchntBaseInfo = new EnterpriseMchntBaseInfo();
            enterpriseMchntBaseInfo.setUploadStatus(status);
            enterpriseMchntBaseInfo.setId(b.getSubmitMsgId());
            if(MessageStatus.DEAL_SUCC.getCode().equals(status)){
                enterpriseMchntBaseInfo.setCenterStatus("01");
            }
            enterpriseMchntBaseInfoMapper.updateEnterpriseMchntBaseInfo(enterpriseMchntBaseInfo);
        }
    }
}
